#ifndef SINGLTEON_H_
#define SINGLETON_H_

template<typename T>
class Singleton {
public:
	static T* instance();
	
	virtual ~Singleton();
	
protected:
	// protected to prohibit direct instantiation, initialization, assignment
	Singleton();
	Singleton(const Singleton&);
	Singleton& operator=(const Singleton&);

private:
	static T* instance_;
};

// template code provided in header file.
// the reason is template object code is invisible across compilation units.

////////////////////////////////////////////////////////////////////////////////
/// The instance
////////////////////////////////////////////////////////////////////////////////
template<typename T>
T* Singleton<T>::instance_ = 0;

template<typename T>
Singleton<T>::Singleton()
{
}

template<typename T>
Singleton<T>::~Singleton()
{
	if( instance_ )
		delete instance_;
}

template<typename T>
T* Singleton<T>::instance()
{
	if( instance_ )
		return instance_;
	return new T();
}

#endif
